package com.zhxy.manager.controller;

import com.alibaba.excel.EasyExcel;
import com.zhxy.manager.entity.Course;
import com.zhxy.manager.entity.Dormitory;
import com.zhxy.manager.service.intf.CourseService;
import com.zhxy.manager.vo.PageBean;
import com.zhxy.manager.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 课程
 * @author ZM
 */
@RestController
@RequestMapping("/api/course/")
public class CourseController {
    @Autowired
    private CourseService service;
    //新增
    @RequestMapping("save.do")
    public R save(Course course) {
        return service.save(course);
    }
    //查询
    @GetMapping("page.do")
    public PageBean page(int page,int limit) {
        return service.queryPage(page,limit);
    }
    //删除
    @PostMapping("del.do")
    public R del(int id) {
        return service.del(id);
    }
    //修改
    @PostMapping("update.do")
    public R update(Course course) {
        return service.update(course);
    }
    //查询  根据条件查询  课程表和教师表联查
    @GetMapping("pagewhere.do")
    public PageBean page(String name,int page,int limit) {
        return service.queryDetail(name,page,limit);
    }

    //数据b表格
    @GetMapping("downall.do")
    public void down(HttpServletResponse response) throws IOException {
        //1.查询数据
        List<Course> list=service.queryAll();
        //2.设置响应消息头
        response.setHeader("Content-disposition", "attachment;filename=" +System.currentTimeMillis() + ".xlsx");
        //3.写出内容
        EasyExcel.write(response.getOutputStream(),Course.class).
                sheet("自动导出").doWrite(list);
    }
    //批量删除  json格式请求
    @PostMapping("delbatch.do")
    public R del(@RequestBody int[] ids) {
        return service.delBatch(ids);
    }

}
